﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyLib {
	/// <summary>
	/// 
	/// </summary>
	public abstract class FileContextProvider {
		/// <summary>
		/// 
		/// </summary>
		public ICacheProvider CacheProvider { get; set; }

		/// <summary>
		/// Get key of cache from name of file.
		/// </summary>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public static string GetCacheKeyByFileName(string fileName) {
			if (string.IsNullOrEmpty(fileName))
				throw new ArgumentNullException("fileName");

			var l = fileName.Length;
			var buffer = new char[l];

			var p = 0;
			if (l > 2) {
				if (fileName[0] == '~' && fileName[1] == '/')
					p = 2;
			}

			var i = 0;
			while (p < l) {
				char c = fileName[p++];
				c = char.ToLower(c);
				if (c == '.')
					c = '_';

				buffer[i++] = c;
			}

			return new string(buffer, 0, i);
		}

		/// <summary>
		/// 
		/// </summary>
		protected FileContextProvider()
			: this(null) {
		} // end of FileDataSourceProvider.

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cacheProvider"></param>
		protected FileContextProvider(ICacheProvider cacheProvider) {
			CacheProvider = cacheProvider;
		}
	} // end of FileDataSourceProvider.
}
